/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package lab.cloud.project.share;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

/**
 *
 * @author daidong
 */
public class LoginView extends JFrame{
    ClientModelInterface model;
    ClientControllerInterface controller;

    JTextField user;
    JButton login;
    JButton Cancel;
    JLabel nullUser;
    JLabel info;
    
    public LoginView(ClientControllerInterface c, ClientModelInterface m){
        model = m;
        controller = c;
    }
    public void createView(){
        setTitle("Login in System");
        setSize(320, 100);
        setLayout(new BorderLayout());

        JPanel all = new JPanel();
        all.setLayout(new BoxLayout(all, BoxLayout.PAGE_AXIS));
        all.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));

        JPanel up = new JPanel();
		up.setLayout(new FlowLayout());
        info = new JLabel();
        info.setText("Your User Name: ");
        user = new JTextField(30);
        up.add(info);
		up.add(user);
        all.add(up);

        nullUser = new JLabel();
        nullUser.setText("Please input your user name");
        nullUser.setVisible(false);
        
        JPanel bp = new JPanel();
		bp.setLayout(new FlowLayout());
		login = new JButton("Login");
		Cancel = new JButton("Cancel");
		ActionListener buttonAction = new UserLoginListener();
		login.addActionListener(buttonAction);
		Cancel.addActionListener(buttonAction);
		bp.add(login);
		bp.add(Cancel);
		all.add(bp);

		setLocationRelativeTo(null);
		getContentPane().add(all, BorderLayout.CENTER);
        pack();
		setResizable(false);
		setVisible(true);
    }
    class UserLoginListener implements ActionListener{
        @Override
		public void actionPerformed(ActionEvent e) {
			String buttonName = e.getActionCommand();
			if ("Login".equals(buttonName)){
				String userName = user.getText();
                if (userName == null){
                    nullUser.setVisible(true);
                    return;
                }
				model.setAuthor(userName);
                dispose();
                ClientView view = new ClientView(controller, model);
                view.createView();
                controller.setClientView(view);
			}
			if ("Cancel".equals(buttonName)){
				dispose();
			}
		}
    }
}
